package com.qf.service.impl;

import com.qf.dao.RotationRepository;
import com.qf.pojo.RedisKey;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.Ratation;
import com.qf.service.PortalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class PortalServiceImpl implements PortalService {

    @Autowired
    RotationRepository rotationRepository;
    @Autowired
    RedisTemplate redisTemplate;

    private static Logger logger= LoggerFactory.getLogger(PortalServiceImpl.class);

    @Override
    public BaseResp findRatation() {
        //1.判断redis中是否有该key
        Boolean aBoolean = redisTemplate.hasKey(RedisKey.RATATION);
        Set result=null;
        if (aBoolean){
            logger.debug("轮播图从redis中获取{}");
            result = redisTemplate.opsForZSet().range(RedisKey.RATATION, 0, -1);
        }else {
            //1.从数据库查询
            logger.debug("轮播图从数据库中获取{}");
            List<Ratation> start = rotationRepository.findByStatus("start");
            for (Ratation ra:start
                 ) {
                redisTemplate.opsForZSet().add(RedisKey.RATATION,ra,ra.getIndex());
            }
            result = redisTemplate.opsForZSet().range(RedisKey.RATATION, 0, -1);
        }


        return new BaseResp(1,"查询成功！",result,null);
    }
}
